import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

class Person implements Cloneable{
    public int id;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class TestDemo {

    //clone、file都是受查异常，必须要有排除异常
    public static void main12(String[] args) {
        System.out.println(readFile());
    }
    public static String readFile() {
        File file = new File("d:/test.txt");
        Scanner sc = null;
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sc.nextLine();
    }

    //throw和throws成对出现
    public static void func4(int x) throws ArithmeticException {
        if(x == 0) {
            throw new ArithmeticException("/ by 0");
        }
    }
    public static void main11(String[] args) {
        try {
            func4(0);
        }catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }

    //finally要避免用return
    public static int func3() {
        int a = 10;
        try {
            //System.out.println(10/0);
            return a;
        }catch (ArithmeticException e) {
            e.printStackTrace();
        }finally {
            return 20;
        }
    }
    public static void main10(String[] args) {
        System.out.println(func3());
    }

    //会优先调用函数的异常
    public static void func2(int n) throws ArithmeticException{
        try {
            System.out.println(10/n);
        }catch (ArithmeticException e) {
            System.out.println("func2()");
            e.printStackTrace();
        }
        // System.out.println(10/n);
    }
    public static void main9(String[] args) {
        try {
            func2(0);
        }catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("main()");
        }
        System.out.println("ffsad");
    }

    //即使有异常没捕捉，finally都会执行
    public static void main8(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int n = scanner.nextInt();
            System.out.println(10 / n);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入有误！");
        } finally {
            //一般用作资源的关闭
            scanner.close();
            System.out.println("finally执行了！");
        }
    }

    //main6的另一种写法
    public static void main7(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            System.out.println(10 / n);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入有误！");
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("算术异常，可能0作为了除数");
        } finally {
            //一般用作 资源的关闭
            System.out.println("finally执行了！");
        }
    }

    //finally
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int n = scanner.nextInt();
            System.out.println(10/n);
        }catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入有误！");
        }catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("算术异常，可能0作为了除数");
        }finally {
            //一般用作 资源的关闭
            scanner.close();
            System.out.println("finally执行了！");
        }
    }

    //递归无退出条件也会异常
    public static void func() {
        func();
    }

    //Exception
    //catch在捕获异常时，要先写子类再写父类
    public static void main5(String[] args) {
        int[] array = {1,2,3};
        try {
            //array = null;
            System.out.println(array[21]);
            System.out.println("haha!");
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("fsafdaf");
        }//不建议这样写
        System.out.println("哈喽！！！");
    }

    //main3的简单写法
    public static void main4(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[2]);
            System.out.println("haha!");
        }catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
            e.printStackTrace();
            System.out.println("我捕捉到了一个数组越界/空指针异常，这里开始处理异常了");
        }
        System.out.println("哈喽！！！");
    }

    //处理多个异常
    public static void main3(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[2]);
            System.out.println("haha!");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("我捕捉到了一个数组越界异常，这里开始处理异常了");
        }catch (NullPointerException e) {//如果不处理就会给jvm处理
            e.printStackTrace();
            System.out.println("捕捉到了空指针异常");
        }
        System.out.println("哈喽！！！");
    }

    //异常基本使用
    //不用try catch， 异常会给jvm处理，程序会立停止
    public static void main2(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[5]);
            System.out.println("haha!");//上面错了,这里不执行
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("我捕捉到了一个数组越界异常，这里开始处理异常了");
        }
        System.out.println("哈喽！！！");//能打印
    }

    //克隆内异常
    public static void main1(String[] args) {
        Person person = new Person();
        try {
            Person person1 = (Person) person.clone();
        }catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

}
